package com.hhd.pdf.component.row;

import com.hhd.pdf.CommonUtils;
import com.hhd.pdf.component.extField.ExtFieldAdapter;
import com.hhd.pdf.component.extField.ExtFieldComponent;
import com.hhd.pdf.component.extField.ExtFieldVo;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.dom4j.Attribute;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class RowComponent extends ExtFieldComponent {
    @Autowired
    ExtFieldAdapter extFieldAdapter;

    @Override
    public List<Element> doGenElements(GeneratorContext context) {
        int colNum = getColNum(context);
        resolveDefaultPadding(context);
        PdfPTable table = createTable(context, colNum);
        if (isCustomAbleRender(context)) {
            customAbleRender(table, colNum, context);
        } else {
            renderCommonCols(table, colNum, context);
        }
        return CommonUtils.newList(table);
    }

    protected void customAbleRender(PdfPTable table, int colNum, GeneratorContext context) {
    }

    private void resolveDefaultPadding(GeneratorContext context) {
        context.getParsingElement().addAttribute("paddingLeft", "30");
    }

    protected boolean isCustomAbleRender(GeneratorContext context) {
        return false;
    }

    private void renderCommonCols(PdfPTable table, int colNum, GeneratorContext context) {
        int count = 0;
        List<org.dom4j.Element> cols = getCols(context);
        for (org.dom4j.Element col : cols) {
            resolveDefaultStyle(col);
            PdfPCell cell = getCell();
            List<Element> elements = genChildren(context, col);
            if (!CommonUtils.isEmpty(elements))
                elements.forEach(cell::addElement);
            table.addCell(cell);
            count++;
        }

        String dataId = getDataId(context);
        if (!CommonUtils.isEmpty(dataId)) {
            List<ExtFieldVo> extFields = extFieldAdapter.getExtFields(dataId, context.getGeneratorParameters().getFormType(), context.getVariableContext());
            for (ExtFieldVo extField : extFields) {
                table.addCell(createExtFieldCell(extField, context));
                count++;
            }
        }

        fillTableCellColumns(table, colNum, count);
    }

    private void resolveDefaultStyle(org.dom4j.Element col) {
        setDefaultAttribute(col, "align", "left");
    }

    protected void fillTableCellColumns(PdfPTable table, int colNum, int count) {
        while (count % colNum != 0) {
            count++;
            table.addCell(getCell());
        }
    }

    protected PdfPCell getCell() {
        PdfPCell cell = PdfUtils.createCell();
        cell.setMinimumHeight(25);
        cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
        cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        return cell;
    }

    static List<org.dom4j.Element> getCols(GeneratorContext context) {
        org.dom4j.Element parsingElement = context.getParsingElement();
        List<org.dom4j.Element> elements = parsingElement.elements();
        return elements.stream().filter(element -> element.getName().equals("col")).collect(Collectors.toList());
    }

    private PdfPTable createTable(GeneratorContext context, int colNum) {
        PdfPTable table = PdfUtils.createTable(colNum, context.getGeneratorParameters().getPageWidth());

        resolveWidth(table, context.getParsingElement(), colNum);
        return table;
    }

    private static void resolveWidth(PdfPTable table, org.dom4j.Element element, int colNum) {
        try {
            List<org.dom4j.Element> elements = element.elements();
            int[] res = new int[colNum];

            for (int i = 0; i < colNum; i++) {
                org.dom4j.Element element1 = elements.get(i);
                Attribute width = element1.attribute("width");
                res[i] = Integer.parseInt(width.getValue());
            }
            table.setWidths(res);
        } catch (Exception e) {
        }
    }

    private static int getColNum(GeneratorContext context) {
        org.dom4j.Element parsingElement = context.getParsingElement();
        String colNumStr = parsingElement.attributeValue("colNum");
        int colNum = 2;
        if (!CommonUtils.isEmpty(colNumStr)) {
            colNum = Integer.parseInt(colNumStr);
        }
        return colNum;
    }

    @Override
    public String getName() {
        return "row";
    }
}
